//
//  PainterUserAVGData.m
//  p4
//
//  Created by CaoJun on 15/5/21.
//  Copyright (c) 2015年 com.baidao. All rights reserved.
//

#import "PainterUserAVGData.h"

using namespace ytx;

@implementation PainterUserAVGData

- (void) drawLine:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ytx::ViewModelIndexPosition *) indexPosition withUserGraph:(ytx::UserGraph) userGraph withDrawSubline:(BOOL)isDrawSubline
{
    AVGData * data = (AVGData *)indexPosition->getData();
    Rectangle rect = indexPosition->getRectangle();

    int dataIndexX1 = userGraph.p1.dataIndexX;
    int dataIndexX2 = userGraph.p2.dataIndexX;

    if (data->drawingCount != data->baseDrawCount * 2) {
        dataIndexX1 -= data->baseDrawCount;
        dataIndexX2 -= data->baseDrawCount;
    }

    if (dataIndexX1 < 0 || dataIndexX2 < 0) {
        return;
    }

    CGFloat x1 = rect.getViewPxByViewIndex(dataIndexX1), y1 = rect.getPxY(userGraph.p1.dataY), x2 = rect.getViewPxByViewIndex(dataIndexX2), y2 = rect.getPxY(userGraph.p2.dataY);

    x1 = Tools::between(rect.getBeginX(), rect.getEndX(), x1);
    y1 = Tools::between(rect.getBeginY(), rect.getEndY(), y1);
    x2 = Tools::between(rect.getBeginX(), rect.getEndX(), x2);
    y2 = Tools::between(rect.getBeginY(), rect.getEndY(), y2);

    [ctx drawLineWithLineWidth:userGraph.lineWidth WidthLineColor:[NSString stringWithUTF8String:userGraph.lineColor.c_str()] withX1:x1 withY1:y1 withX2:x2 withY2:y2];
    [ctx clipToRect:CGRectMake(rect.getBeginX(), rect.getBeginY(), rect.getRectWidth(), rect.getRectHeight())];

    if (isDrawSubline) {
        [self drawSubline:cgrect withContext:ctx withViewModelIndexPosition:indexPosition withPoint1:CGPointMake(x1, y1) withDataIndex1:dataIndexX1 withPoint2:CGPointMake(x2, y2) withDataIndex2:dataIndexX2];
    }
}

- (void) drawRectangle:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ytx::ViewModelIndexPosition *) indexPosition withUserGraph:(ytx::UserGraph) userGraph withDrawSubline:(BOOL)isDrawSubline
{
    AVGData * data = (AVGData *)indexPosition->getData();
    Rectangle rect = indexPosition->getRectangle();

    int dataIndexX1 = userGraph.p1.dataIndexX;
    int dataIndexX2 = userGraph.p2.dataIndexX;

    if (data->drawingCount != data->baseDrawCount * 2) {
      dataIndexX1 -= data->baseDrawCount;
      dataIndexX2 -= data->baseDrawCount;
        //分时
    }

    if (dataIndexX1 < 0 || dataIndexX2 < 0) {
        return;
    }

    CGFloat x1 = rect.getViewPxByViewIndex(dataIndexX1), y1 = rect.getPxY(userGraph.p1.dataY), x2 = rect.getViewPxByViewIndex(dataIndexX2), y2 = rect.getPxY(userGraph.p2.dataY);

    if (isDrawSubline) {
        [self drawSubline:cgrect withContext:ctx withViewModelIndexPosition:indexPosition withPoint1:CGPointMake(x1, y1) withDataIndex1:dataIndexX1 withPoint2:CGPointMake(x2, y2) withDataIndex2:dataIndexX2];
    }

    [ctx drawRectWithX1:x1 withY1:y1 withX2:x2 withY2:y2 withLineWidth:userGraph.lineWidth withLineColor:[NSString stringWithUTF8String:userGraph.lineColor.c_str()]];
    [ctx clipToRect:CGRectMake(rect.getBeginX(), rect.getBeginY(), rect.getRectWidth(), rect.getRectHeight())];
}

- (void) drawSubline:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ytx::ViewModelIndexPosition *) indexPosition withPoint1:(CGPoint) point1 withDataIndex1:(int) dataIndexX1 withPoint2:(CGPoint) point2 withDataIndex2:(int) dataIndexX2
{
    AVGData * data = (AVGData *)indexPosition->getData();
    [self drawCrosshairsX:cgrect withContext:ctx withViewModelIndexPosition:indexPosition withPx:point2.x];
    [self drawCrosshairsY:cgrect withContext:ctx withViewModelIndexPosition:indexPosition withPx:point2.y];
    NSString * textY2 = [NSString stringWithUTF8String:Tools::Convert2FixString( indexPosition->getRectangle().getValueY(point2.y), indexPosition->getData()->getToFix() ).c_str()];
    [self drawCrosshairsYText:cgrect withContext:ctx withViewModelIndexPosition:indexPosition withPx:point2.y withText:textY2 withValue:indexPosition->getRectangle().getValueY(point2.y)];
    
    NSString * textX2 = [NSString stringWithUTF8String: data->getTimeDurationByIndex(dataIndexX2).toStringHHmm().c_str() ];
    [self drawCrosshairsXText:cgrect withContext:ctx withViewModelIndexPosition:indexPosition withPx:point2.x withText:textX2];
    
    [self drawCrosshairsX:cgrect withContext:ctx withViewModelIndexPosition:indexPosition withPx:point1.x];
    [self drawCrosshairsY:cgrect withContext:ctx withViewModelIndexPosition:indexPosition withPx:point1.y];
    NSString * textY1 = [NSString stringWithUTF8String:Tools::Convert2FixString( indexPosition->getRectangle().getValueY(point1.y), indexPosition->getData()->getToFix() ).c_str()];
    [self drawCrosshairsYText:cgrect withContext:ctx withViewModelIndexPosition:indexPosition withPx:point1.y withText:textY1 withValue:indexPosition->getRectangle().getValueY(point1.y)];
    
    NSString * textX1 = [NSString stringWithUTF8String: data->getTimeDurationByIndex(dataIndexX1).toStringHHmm().c_str() ];
    [self drawCrosshairsXText:cgrect withContext:ctx withViewModelIndexPosition:indexPosition withPx:point1.x withText:textX1];
}

- (void)drawCrosshairsYText:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ViewModelIndexPosition *) indexPosition withPx:(CGFloat) px withText:(NSString *) text withValue:(double) value
{
    [super drawCrosshairsYText:cgrect withContext:ctx withViewModelIndexPosition:indexPosition withPx:px withText:text];
    
    AVGData * data = (AVGData *) indexPosition->getData();
    
    double prevValue = data->prevValue;
    
    YTXConfig * config = YTXConfig::getInstance();
    ytx::Style style = config->style;
    Rectangle & rect = indexPosition->getRectangle();
    
    NSString * font = [NSString stringWithUTF8String:style.crosshairsTextFont.c_str()];
    
    NSString * rightText = [NSString stringWithUTF8String:Tools::numberToFixedPercentString(fabs((value * 100 / prevValue) - 100)).c_str()];
    
    CGRect backgroundRect = [ctx getTextRect:rightText withFont:font withSize:style.crosshairsTextSize];
    backgroundRect.size.width+=2;
    backgroundRect.size.height+=2;
    backgroundRect.origin.x = rect.getEndX()-backgroundRect.size.width;
    backgroundRect.origin.y = px-backgroundRect.size.height*0.5;
    
    [ctx fillRect:backgroundRect withFillColor:[NSString stringWithUTF8String:style.crosshairsTextBackgorundColor.c_str()]];
    
    [ctx fillText:rightText withX:rect.getEndX() withY:px withColor:[NSString stringWithUTF8String:style.crosshairsTextColor.c_str()] withFont:font withSize:style.crosshairsTextSize withAnchor:RightMiddle];
    
}

@end
